#import "sprite_game_bow.h"

@implementation SpriteGameBow

@synthesize bowBaseLength = bowBaseLength_;
@synthesize dragDistance = dragDistance_;

- (id)initSpriteGameBow:(int)Type
{
	if ((self = [super init]))
	{
		image_ = nil;
		[self SetCurrentUseBow:Type];
	}
	return self;
}

- (void)draw
{
	dragPoint_ = ccp(0, -bowBaseLength_ - dragDistance_);
	glColor4f(0, 0, 0, 1.0f);
	glLineWidth(3.0f);
	uiRenderLine(leftPoint_, dragPoint_);
	uiRenderLine(rightPoint_, dragPoint_);
}

- (void)Pause
{
	[self pauseSchedulerAndActions];
	[image_ pauseSchedulerAndActions];
}

- (void)Resume
{
	[self resumeSchedulerAndActions];
	[image_ resumeSchedulerAndActions];
}

- (void)SetCurrentUseBow:(int)Type
{
	NSString *_Path;
	CCSpriteFrame *_Frame;

	type_ = Type;

	/*set display frame*/
	_Path = [NSString stringWithUTF8String:g_CfgGameBowDataTable[type_].image_path];
	_Frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:_Path];
	if (nil == image_)
	{
		image_ = [[CCSprite alloc] initWithSpriteFrameName:_Path];
		[self addChild:image_];
		[image_ release];
	}
	else
	{
		[image_ setDisplayFrame:_Frame];
	}

	imageAnchorPoint_ = ccp(0.5, (float)g_CfgGameBowDataTable[type_].anchor_point_to_bottom / (float)g_CfgGameBowDataTable[type_].image_size.height);
	image_.anchorPoint = imageAnchorPoint_;

	bowBaseLength_ = fabs(g_CfgGameBowDataTable[type_].left_point.y);
	leftPoint_ = g_CfgGameBowDataTable[type_].left_point;
	rightPoint_ = g_CfgGameBowDataTable[type_].right_point;

	[self setRotation:0.0f];
	[self SetDefaultDragPoint];
	dragDistance_ = 0.0f;

	[self PlayAniNormal];
}

- (void)SetDefaultDragPoint
{
	dragPoint_ = ccp(0.0f, -bowBaseLength_);
}

- (int)GetArrowType
{
	return g_CfgGameBowDataTable[type_].arrow_type;
}

- (CGPoint)GetBowHandlePoint
{
	CGPoint _Ret;

	_Ret.x = 0;
	_Ret.y = -(bowBaseLength_ + dragDistance_);
	_Ret = ptRotatePoint_CGPoint(_Ret, -self.rotation);
	_Ret = ccpAdd(_Ret, self.positionInPixels);
	return _Ret;
}

- (void)ActionPreview
{
	[self runAction:[CCFadeIn actionWithDuration:MISSION_START_PLAY_TIME]];
}

- (void)PlayAniNormal
{
	CCAnimation *_Animation;

	_Animation = uiCreateAnimationObject(&g_CfgGameBowDataTable[type_].ani_normal);
	if (nil != _Animation)
	{
		[image_ runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:_Animation]]];
	}
}

- (void)PlayAniRelease
{
	CCSequence *_Seq;
	CCAnimation *_Animation;
	id _Action;
	CCCallFuncN *_CallBackAniOver;

	_Animation = uiCreateAnimationObject(&g_CfgGameBowDataTable[type_].ani_release);
	if (nil != _Animation)
	{
		[image_ stopAllActions];
		_Action = [CCAnimate actionWithAnimation:_Animation];
		_CallBackAniOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackAniReleaseOver:)];
		_Seq = [CCSequence actions:_Action, _CallBackAniOver, nil];
		[image_ runAction:_Seq];
	}
	else
	{
		[self CallBackAniReleaseOver:nil];
	}
}

- (void)CallBackAniReleaseOver:(id)Sender
{
	[image_ setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithUTF8String:g_CfgGameBowDataTable[type_].image_path]]];
	[self PlayAniNormal];
	image_.anchorPoint = imageAnchorPoint_;
}

@end